package demo04;

import java.util.ArrayList;
import java.util.function.Predicate;

/*
 *  Predicate接口案例：集合信息筛选
 */
public class Demo05PredicateFilter {
    public static void main(String[] args) {
        String[] strings = { "土司鸡,男", "老虎,女", "粹花,女", "玉玉子,女" };
        /*
         * 筛选条件：必须为女生，姓名必须为2个字
         */

        for (String string : stringFilter(strings,
                string -> string.split(",")[1].equals("女"),
                string -> string.split(",")[0].length() == 2)) {
            System.out.println(string); // 老虎,女 粹花,女
        }
    }

    public static ArrayList<String> stringFilter(String[] strings, Predicate<String> predicate1,
            Predicate<String> predicate2) {
        ArrayList<String> arrayList = new ArrayList<>();
        for (String string : strings) {
            if (predicate1.and(predicate2).test(string)) {
                arrayList.add(string);
            }
        }
        return arrayList;
    }
}
